﻿using System;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Generic;

namespace WSOAMOBIMIDWARE
{
    [XmlRootAttribute("root")]
    public class HTTPOA_Action
    {
        [XmlAttribute("ID")]
        public string actionId { get; set; }
        [XmlElementAttribute("key")]
        public string key { get; set; }
        [XmlElementAttribute("confirm")]
        public HTTPOA_PageConfirm pageConfirm { get; set; }
        [XmlElementAttribute("prompt")]
        public HTTPOA_PagePrompt pagePrompt { get; set; }
        [XmlElementAttribute("notedited")]
        public string notedited { get; set; }
        [XmlElementAttribute("url")]
        public string url { get; set; }
        [XmlElementAttribute("goback")]
        public string goback { get; set; }
        [XmlElementAttribute("fun")]
        public string fun { get; set; }
        [XmlElementAttribute("process")]
        public HTTPOA_PageProgress progress { get; set; }
        [XmlElementAttribute("title")]
        public HTTPOA_PageTitle title { get; set; }
        [XmlElementAttribute("listfield")]
        public string listfield { get; set; }
        [XmlElementAttribute("argument")]
        public HTTPOA_PostField[] argument { get; set; }
        [XmlElementAttribute("pname")]
        public string pname { get; set; }
        [XmlElementAttribute("phname")]
        public string phname { get; set; }
        [XmlElementAttribute("pvalue")]
        public string pvalue { get; set; }
        [XmlElementAttribute("datas")]
        public HTTPOA_PageSelectDataSet selectDataSet { get; set; }
        [XmlElementAttribute("next")]
        public string next { get; set; }

        public static void CastXmlToType(object value, out HTTPOA_Action action, out string actionId)
        {
            action = null;
            actionId = string.Empty;
            if (value == null)
            {
                return ;
            }
            if (value.GetType().Equals(typeof(HTTPOA_Action)))
            {
                action = (HTTPOA_Action)value;
            }
            else if (value.GetType().Equals(typeof(string)))
            {
                actionId = value.ToString();
            }
            else
            {
                XmlNode[] setValue = (XmlNode[])value;
                if (setValue.Length == 1 && setValue[0].NodeType.ToString().Equals("Text"))
                {
                    actionId = setValue[0].Value;
                }
                else
                {
                    action = new HTTPOA_Action();
                    List<HTTPOA_PostField> argumentItems = new List<HTTPOA_PostField>();
                    for (int i = 0; i < setValue.Length; i++)
                    {
                        if (setValue[i].Name.Equals("ID"))
                        {
                            action.actionId = setValue[i].Value;
                        }
                        else if (setValue[i].Name.Equals("key"))
                        {
                            action.key = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("confirm"))
                        {
                            action.pageConfirm = new HTTPOA_PageConfirm();
                            foreach (XmlNode confirmNode in setValue[i].ChildNodes)
                            {
                                if (confirmNode.Name.Equals("img"))
                                {
                                    action.pageConfirm.img = confirmNode.FirstChild.Value;
                                }
                                else if (confirmNode.Name.Equals("char"))
                                {
                                    action.pageConfirm.message = confirmNode.FirstChild.Value;
                                }
                            }
                        }
                        else if (setValue[i].Name.Equals("prompt"))
                        {
                            action.pagePrompt = new HTTPOA_PagePrompt();
                            foreach (XmlNode promptNode in setValue[i].ChildNodes)
                            {
                                if (promptNode.Name.Equals("img"))
                                {
                                    action.pagePrompt.img = promptNode.FirstChild.Value;
                                }
                                else if (promptNode.Name.Equals("char"))
                                {
                                    action.pagePrompt.message = promptNode.FirstChild.Value;
                                }
                            }
                        }
                        else if (setValue[i].Name.Equals("notedited"))
                        {
                            action.notedited = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("url"))
                        {
                            action.url = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("goback"))
                        {
                            action.goback = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("fun"))
                        {
                            action.fun = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("process"))
                        {
                            action.progress = new HTTPOA_PageProgress();
                            foreach (XmlNode progressNode in setValue[i].ChildNodes)
                            {
                                if (progressNode.Name.Equals("img"))
                                {
                                    action.progress.img = progressNode.FirstChild.Value;
                                }
                                else if (progressNode.Name.Equals("char"))
                                {
                                    action.progress.message = progressNode.FirstChild.Value;
                                }
                                else if (progressNode.Name.Equals("speed"))
                                {
                                    action.progress.speed = progressNode.FirstChild.Value;
                                }
                            }
                        }
                        else if (setValue[i].Name.Equals("title"))
                        {
                            action.title = new HTTPOA_PageTitle();
                            foreach (XmlNode titleNode in setValue[i].ChildNodes)
                            {
                                if (titleNode.Name.Equals("img"))
                                {
                                    action.title.img = titleNode.FirstChild.Value;
                                }
                                else if (titleNode.Name.Equals("char"))
                                {
                                    action.title.message = titleNode.FirstChild.Value;
                                }
                                else if (titleNode.Name.Equals("desc"))
                                {
                                    action.title.desc = titleNode.FirstChild.Value;
                                }
                            }
                        }
                        else if (setValue[i].Name.Equals("listfield"))
                        {
                            action.listfield = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("argument"))
                        {
                            HTTPOA_PostField argumentItem = new HTTPOA_PostField();
                            List<string> values = new List<string>();
                            foreach (XmlNode argumentNode in setValue[i].ChildNodes)
                            {
                                if (argumentNode.Name.Equals("name"))
                                {
                                    argumentItem.name = argumentNode.FirstChild.Value;
                                }
                                else if (argumentNode.Name.Equals("value"))
                                {
                                    values.Add(argumentNode.FirstChild.Value);
                                }
                            }
                            argumentItem.values = values.ToArray();
                            argumentItems.Add(argumentItem);
                        }
                        else if (setValue[i].Name.Equals("pname"))
                        {
                            action.pname = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("phname"))
                        {
                            action.phname = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("pvalue"))
                        {
                            action.pvalue = setValue[i].FirstChild.Value;
                        }
                        else if (setValue[i].Name.Equals("datas"))
                        {
                            action.selectDataSet = new HTTPOA_PageSelectDataSet();
                            List<HTTPOA_PageSelectDataItem> dataItems = new List<HTTPOA_PageSelectDataItem>();
                            foreach (XmlNode dataNode in setValue[i].ChildNodes)
                            {
                                if (dataNode.Name.Equals("type"))
                                {
                                    action.selectDataSet.type = dataNode.FirstChild.Value;
                                }
                                else if (dataNode.Name.Equals("data"))
                                {
                                    HTTPOA_PageSelectDataItem dataItem = new HTTPOA_PageSelectDataItem();
                                    foreach (XmlNode dataItemNode in dataNode.ChildNodes)
                                    {

                                        if (dataItemNode.Name.Equals("img"))
                                        {
                                            dataItem.img = dataItemNode.FirstChild.Value;
                                        }
                                        else if (dataItemNode.Name.Equals("char"))
                                        {
                                            dataItem.message = dataItemNode.FirstChild.Value;
                                        }
                                        else if (dataItemNode.Name.Equals("value"))
                                        {
                                            dataItem.value = dataItemNode.FirstChild.Value;
                                        }
                                        else if (dataItemNode.Name.Equals("hvalue"))
                                        {
                                            dataItem.hvalue = dataItemNode.FirstChild.Value;
                                        }
                                    }
                                    dataItems.Add(dataItem);
                                }
                            }
                            action.selectDataSet.datas = dataItems.ToArray();
                        }
                        else if (setValue[i].Name.Equals("next"))
                        {
                            action.next = setValue[i].FirstChild.Value;
                        }
                    }
                    action.argument = argumentItems.ToArray();
                }
            }
        }
    }
}
